﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace SEO.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 代理访问百度类
    /// 完成日期：2011年6月12日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class Proxy
    {
        public static int iSuccess = 0;
        public static int iFail = 0;

        /// <summary>
        /// 通过代理访问百度
        /// </summary>
        /// <param name="strUrl">访问链接</param>
        /// <param name="strProxyIPAddress">代理服务器IP</param>
        /// <param name="iProxyPort">代理服务器端口</param>
        /// <returns>返回状态</returns>
        public static string PostBaiduByProxy(string strUrl,string strProxyIPAddress,int iProxyPort)
        {
            string strReturnValue = "";

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                //设置一个代理 
                WebProxy proxy = new WebProxy(strProxyIPAddress, iProxyPort);
                request.Proxy = proxy;
                HttpWebResponse response = null;

                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                    iSuccess++;
                }
                catch (Exception ex)
                {
                    strReturnValue = ex.Message;
                    iFail++;
                }

                //判断HTTP响应状态 
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    strReturnValue = "失败";
                }
                else
                {
                    strReturnValue = "成功";
                    //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
                    //string strHtml = reader.ReadToEnd();
                    //reader.Close();
                }
                response.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("通过代理访问百度 -> " + ex.Message);
            }

            return strReturnValue;
        }


        /// <summary>
        /// 通过客户端互刷访问百度
        /// </summary>
        /// <param name="strUrl">访问链接</param>
        /// <returns>返回状态</returns>
        public static string PostBaiduByClient(string strUrl)
        {
            string strReturnValue = "";

            try
            {
                //
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("通过代理访问百度 -> " + ex.Message);
            }

            return strReturnValue;
        }

    }
}
